home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
World of Education
/
World of Education.iso
/
world_z
/
zphere.zip
/
ZPHERE.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1993-01-06
|
8KB
|
383 lines
/**
* Zphere - Zapping sphere is a mostly harmless visual display
* of a simplifiled Van de Graf type of simulation.
*
* by Gil Nardo @Migrant Computing Services, Jan 1992
**/
#include <fstream.h>
#include <stdlib.h>
#include <math.h>
#include <dos.h> // for sleep()
#include <time.h> // for randomize()
#include <string.h> // for strcpy()
#include <graphics.h>
#include <conio.h>
#define BGIDIRECTORY "c:\\borlandc\\bgi"
int nowpen = 2;
int max_x, max_y, mid_y, mid_x, min_x, min_y, size_x, size_y;
void
setlimits()
{
min_x = 0;
min_y = 0;
max_x = getmaxx();
max_y = getmaxy();
size_x = max_x - min_x + 1;
size_y = max_y - min_y + 1;
mid_y = size_y / 2;
mid_x = size_x / 2;
}
void
open_scene()
{
// int gdriver = DETECT, gmode = VGAHI, errorcode;
int gdriver, gmode, errorcode;
char usebgidir[80];
int maxtry;
// use this section if you have access to egavga.obj
// otherwise comment next 6 lines
errorcode = registerbgidriver(EGAVGA_driver);
if (registerbgidriver(EGAVGA_driver) < 0)
{
cerr << "Graphics error: " << grapherrormsg(errorcode) << '\n';
exit(1);
}
*usebgidir = NULL;
for(maxtry = 0; ; maxtry++)
{
gdriver = DETECT;
gmode = VGAHI;
initgraph(&gdriver, &gmode, usebgidir);
errorcode = graphresult();
if (errorcode == grOk)
break;
if (maxtry == 0)
{
strcpy(usebgidir, BGIDIRECTORY);
continue;
}
if (maxtry > 3)
{
cerr << "Graphics Error: " << grapherrormsg(errorcode) << ".\n";
exit(1);
}
if (errorcode == grFileNotFound)
{
cout << "Requires Borland Graphics Interface (BGI) modules\n";
cout << "Please enter the BGI path: ";
cin >> usebgidir;
}
else
{
cerr << "Graphics Error: " << grapherrormsg(errorcode) << ".\n";
exit(1);
}
}
setlimits();
return;
}
void
close_scene()
{
closegraph();
cout << "Zphere = Zapping Sphere\n";
return;
}
void
plotpen(int whichcolor)
{
nowpen = whichcolor;
setcolor(whichcolor);
return;
}
main()
{
register long index;
int xscan, yscan;
int numpoints;
int lastx;
int useradius;
int usersquared;
int addx, addy, usex, usey, signx, signy;
int quartered;
int xwander, ywander;
struct pointtype *thunder;
open_scene();
randomize();
useradius = (size_x < size_y) ? size_x : size_y;
useradius /= 4;
usersquared = useradius * useradius;
quartered = useradius * 3 / 4;
xwander = (size_x / 2 - useradius) / 3;
ywander = (size_y / 2 - useradius) / 3;
for(index = 0; index < 20000; index++)
{
plotpen(random(MAXCOLORS - 1));
putpixel(random(max_x), random(max_y), nowpen);
}
thunder = new(struct pointtype [size_y * 4]);
plotpen(BLUE);
outtextxy(mid_x - mid_x / 4, mid_y, "Zphere by Gil Nardo");
plotpen(YELLOW);
outtextxy(mid_x - mid_x / 4 - 2, mid_y + 1, "Zphere by Gil Nardo");
for(;;)
{
signx = (random(2) == 1) ? 1 : -1;
signy = (random(2) == 1) ? 1 : -1;
usex = random(useradius + 20);
usex = (usex >= useradius) ? useradius : usex;
usey = sqrt(usersquared - usex * usex);
numpoints = 0;
thunder[numpoints].x = mid_x + signx * usex;
thunder[numpoints].y = mid_y - signy * usey;
usex = thunder[numpoints].x;
usey = thunder[numpoints].y;
numpoints++;
if (signx < 0)
{
if (signy < 0)
{ // lower left
if (usex < mid_x - quartered)
{ // zap horizontal
addy = usey;
xscan = usex;
for(; xscan >= 0; xscan -= 8)
{
if (random(2) == 1)
{
if (random(2) == 1)
addy -= random(ywander);
else
addy += random(ywander);
if (addy < usey)
addy = usey;
if (addy > max_y)
addy = max_y;
thunder[numpoints].x = xscan;
thunder[numpoints].y = addy;
numpoints++;
}
}
}
else
{ // zap vertical
addx = usex;
yscan = usey;
for(; yscan <= max_y; yscan += 8)
{
if (random(2) == 1)
{
if (random(2) == 1)
addx -= random(xwander);
else
addx += random(xwander);
if (addx > usex)
addx = usex;
if (addx < 0)
addx = 0;
thunder[numpoints].x = addx;
thunder[numpoints].y = yscan;
numpoints++;
}
}
}
}
else
{ // upper left
if (usex < mid_x - quartered)
{ // zap horizontal
addy = usey;
xscan= usex;
for(; xscan >= 0; xscan -= 8)
{
if (random(2) == 1)
{
if (random(2) == 1)
addy -= random(ywander);
else
addy += random(ywander);
if (addy > usey)
addy = usey;
if (addy < 0)
addy = 0;
thunder[numpoints].x = xscan;
thunder[numpoints].y = addy;
numpoints++;
}
}
}
else
{ // zap vertical
addx = usex;
yscan = usey;
for(; yscan >= 0; yscan -=8)
{
if (random(2) == 1)
{
if (random(2) == 1)
addx -= random(xwander);
else
addx += random(xwander);
if (addx > usex)
addx = usex;
if (addx < 0)
addx = 0;
thunder[numpoints].x = addx;
thunder[numpoints].y = yscan;
numpoints++;
}
}
}
}
}
else // assume signx >= 0
{
if (signy < 0)
{ // lower right
if (usex < mid_x + quartered)
{ // zap vertical
addx = usex;
yscan = usey;
for(; yscan <= max_y; yscan += 8)
{
if (random(2) == 1)
{
if (random(2) == 1)
addx -= random(xwander);
else
addx += random(xwander);
if (addx < usex)
addx = usex;
if (addx > max_x)
addx = max_x;
thunder[numpoints].x = addx;
thunder[numpoints].y = yscan;
numpoints++;
}
}
}
else
{ // zap horizontal
addy = usey;
xscan= usex;
for(; xscan <= max_x; xscan += 8)
{
if (random(2) == 1)
{
if (random(2) == 1)
addy -= random(ywander);
else
addy += random(ywander);
if (addy < usey)
addy = usey;
if (addy > max_y)
addy = max_y;
thunder[numpoints].x = xscan;
thunder[numpoints].y = addy;
numpoints++;
}
}
}
}
else
{ // upper right
if (usex < mid_x + quartered)
{ // zap vertical
addx = usex;
yscan = usey;
for(; yscan >= 0; yscan -= 8)
{
if (random(2) == 1)
{
if (random(2) == 1)
addx -= random(xwander);
else
addx += random(xwander);
if (addx < usex)
addx = usex;
if (addx > max_x)
addx = max_x;
thunder[numpoints].x = addx;
thunder[numpoints].y = yscan;
numpoints++;
}
}
}
else
{ // zap horizontal
addy = usey;
xscan= usex;
for(; xscan <= max_x; xscan += 8)
{
if (random(2) == 1)
{
if (random(2) == 1)
addy -= random(ywander);
else
addy += random(ywander);
if (addy > usey)
addy = usey;
if (addy < 0)
addy = 0;
thunder[numpoints].x = xscan;
thunder[numpoints].y = addy;
numpoints++;
}
}
}
}
}
plotpen(WHITE);
drawpoly(numpoints, (int *)thunder);
if (kbhit())
break;
delay(25);
plotpen(RED);
drawpoly(numpoints, (int *)thunder);
delay(25);
plotpen(BLACK);
drawpoly(numpoints, (int *)thunder);
if (numpoints % 2)
{ // allow births
plotpen(1 + random(MAXCOLORS - 2)); // ignore blackout
putpixel(random(max_x), random(max_y), nowpen);
}
}
close_scene();
delete(thunder);
return(0);
}